package com.yenlien.traceyun.milk.service;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.rest.usercenter.CompanyRestService;
import com.yenlien.traceyun.business.service.rest.usercenter.UserRestService;
import com.yenlien.traceyun.milk.dao.MilkManageDao;
import com.yenlien.traceyun.milk.entity.MilkManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2022/10/17
 * @Author jiaLong
 * @Descript
 */
@Service
public class MilkManageService {

    @Autowired
    private MilkManageDao milkManageDao;
    @Autowired
    private CompanyRestService companyRestService;
    @Autowired
    private UserRestService userRestService;


    public MilkManage save(MilkManage milkManage) {
        if(StringUtils.isEmpty(milkManage.getId())){
            JSONObject company = JSONObject.parseObject(companyRestService.findById(UserAuthUtil.getLoginUser().getCompanyId()).toString()).getJSONObject("data");
            milkManage.setCompanyId(company.getString("id"));
            milkManage.setCompanyName(company.getString("abbreviation"));
            Map<String, Object> user = new HashMap<>();
            user.put("companyId", company.getString("id"));
            user.put("phone", milkManage.getPhone());
            user.put("username", milkManage.getName());
            user.put("password", milkManage.getPassword());
            user.put("name", milkManage.getName());
            user.put("isManage", "0");
            userRestService.save(user, null);
        }
        return milkManageDao.save(milkManage);
    }

    public List<MilkManage> findAll() {
        return milkManageDao.findAll();
    }


    public MilkManage findById(String id) {
        return milkManageDao.findById(id);
    }


    public Page<MilkManage> findByList(MilkManage milkManage) {
        Pageable pageable = PageRequest.of(milkManage.getPage(), milkManage.getPageSize(), Sort.Direction.DESC, "createtime");
        return milkManageDao.findAll((Root<MilkManage> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            List<Predicate> list = new ArrayList<>();
            if (!StringUtils.isEmpty(milkManage.getCompanyId())) {
                list.add(cb.equal(root.get("companyId"), milkManage.getCompanyId()));
            }
            if (!StringUtils.isEmpty(milkManage.getName())) {
                list.add(cb.like(root.get("name"), "%" + milkManage.getName() + "%"));
            }
            if (!StringUtils.isEmpty(milkManage.getCompanyName())) {
                list.add(cb.equal(root.get("companyName"), "%" + milkManage.getCompanyName() + "%"));
            }
            if (!StringUtils.isEmpty(milkManage.getState())) {
                list.add(cb.equal(root.get("state"), milkManage.getCompanyId()));
            }
            Predicate[] predicate = new Predicate[list.size()];
            return cb.and(list.toArray(predicate));
        }, pageable);
    }







}
